//
//  MessageLayer.m
//  SquirrelAttack
//
//  Created by BulletHermit on 13-2-2.
//  Copyright 2013年 Nothing. All rights reserved.
//

#import "MessageLayer.h"
#import "SimpleAudioEngine.h"
#import "AppDelegate.h"
#import <CoreData/CoreData.h>

@implementation MessageLayer
@synthesize bg;
+(id) nodeWith:(CCLayer *)bind
{
    return [[[self alloc] initWith:bind] autorelease];
}
-(id) initWith:(CCLayer*)bind
{
    if (self=[super init]) {
        CGSize screenSize=[[CCDirectorIOS sharedDirector] winSize];
        self.position= ccp(screenSize.width/2,screenSize.height/2);
        
        bg=[CCSprite spriteWithFile:@"bg_msg.png"];
        [self addChild:bg z:0];

        

    }
    return self;
}
-(id) init
{
    if (self=[super init]) {
        CGSize screenSize=[[CCDirectorIOS sharedDirector] winSize];
        self.position= ccp(screenSize.width/2,screenSize.height/2);
        
        bg=[CCSprite spriteWithFile:@"bg_msg.png"];
        [self addChild:bg z:0];
        CCLOG(@"%@",NSStringFromCGRect(bg.boundingBox));
        
        bgMusicVolume = [CCSlider sliderWithCCSlider:@"sliderButton.png" background:@"sliderBg.png" target:self callback:@selector(changeBgSoundVolume)];
        bgMusicVolume.position=ccp(-60,0);
        [self addChild:bgMusicVolume z:1];
        
        effectsVolume = [CCSlider sliderWithCCSlider:@"sliderButton.png" background:@"sliderBg.png" target:self callback:@selector(changeEffectsVolume)];
        effectsVolume.position=ccp(-60,-50);
        [self addChild:effectsVolume z:1];
        
        CCLabelTTF* title = [CCLabelTTF labelWithString:@"Setting" fontName:@"MarkerFelt-Thin" fontSize:20];
        title.color=(ccColor3B){0,0,0};
        
        title.position=ccp(0,27);
        [self addChild:title z:1];
        CCLOG(@"Effects Volume:%f",[self volumeForKey:@"sound"]);

        [effectsVolume setCurValue:[self volumeForKey:@"sound"]*effectsVolume.maxValue];
        CCLOG(@"Music Volume:%f",[self volumeForKey:@"music"]);
        [bgMusicVolume setCurValue:[self volumeForKey:@"music"]*bgMusicVolume.maxValue];
        [self changeBgSoundVolume];
        [self changeEffectsVolume];
        
        
    }
    return self;
}
-(float) volumeForKey:(NSString*)key
{

    AppController *appDelegate =
    (AppController*)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription
                                              entityForName:@"Volume"
                                              inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];
    
    NSError *error;
    NSArray *objects = [context executeFetchRequest:request error:&error];
    if (objects == nil) {
        NSLog(@"There was an error!");
        // Do whatever error handling is appropriate
    }
    
    for (NSManagedObject *oneObject in objects) {
        NSString *theKind = [oneObject valueForKey:@"kind"];
        NSNumber *theValue = [oneObject valueForKey:@"value"];
        if (![theKind compare: key]) {
        
            return [theValue floatValue];
        }
        
        
    }
    [context save:&error];
    return 0;
    
}

-(void) changeEffectsVolume
{
   
    [[SimpleAudioEngine sharedEngine] setEffectsVolume: effectsVolume.curValue/effectsVolume.maxValue ];
    
    
    
}
-(void) changeBgSoundVolume
{
    [[SimpleAudioEngine sharedEngine] setBackgroundMusicVolume: bgMusicVolume.curValue/bgMusicVolume.maxValue ];
    
}
@end
